<?php

require "conf.php";
require "Constants.php";
require_once "utils/Validate.php";

/**
 * This class is a proxy for retrieving game data from
 * the amazon web service. This class requires PHP5 
 */
 class AWSProxy{
	
	public function AWSProxy(){
		
	}
	
	public function getAllWiiGames($locale){
	
		Validate::notNull($locale, "attempting to get games, when locale not defined");

		$all_games = array();
		$totalPages = $this->getTotalPages($locale);
		
		for($i=1; $i< $totalPages; $i++){
			
			$page = $this->getPage($i, $locale);

			foreach($page->Items->Item as $g){

				$id = $g->ASIN;
				$title = str_replace("(Wii)", "", $g->ItemAttributes->Title);
				$manufacturer = $g->ItemAttributes->Publisher;
				$release_date = $g->ItemAttributes->ReleaseDate;
				$image_url = $g->SmallImage->URL;
				$largeImageUrl = $g->LargeImage->URL;
				$amazon_url = $g->DetailPageURL;
				$description = $g->EditorialReviews->EditorialReview->Content;

				$game = array('id' => $id, 
							  'title' => $title, 
							  'manufacturer' => $manufacturer,
							  'release_date' => $release_date,
							  'image_url' => $image_url,
							  'largeImageUrl' => $largeImageUrl, 
							  'amazon_url' => $amazon_url,
							  'description' => $description);
			
				array_push($all_games,$game);
			}
		}
		return $all_games;
		
	}

	/**
	 * amazon item search for .co.uk (UK locale)
	 */
	private function AWSUKItemSearch($page){

		$Operation = "ItemSearch";
		$Version = "2008-03-03";
		$ResponseGroup = "ItemAttributes,Images,EditorialReview";

		$request=
	     "http://ecs.amazonaws.co.uk/onca/xml"
   	    . "?Service=AWSECommerceService"
	   	. "&AssociateTag=" . AWS_ASSOCIATE_TAG
	   	. "&AWSAccessKeyId=" . AWS_ACCESS_KEY
   		. "&Operation=" . $Operation
   		. "&Version=" . $Version
   		. "&SearchIndex=VideoGames"
   		. "&BrowseNode=16377051"
   		. "&ResponseGroup=" . $ResponseGroup
   		. "&ItemPage=" . $page;

		$response = file_get_contents($request);
		$parsed_xml = simplexml_load_string($response);
		return $parsed_xml;
	}

	/**
	 * amazon item search for .com (US locale)
	 */
	private function AWSUSItemSearch($page){

		$Operation = "ItemSearch";
		$Version = "2008-03-03";
		$ResponseGroup = "ItemAttributes,Images,EditorialReview";

		$request=
	     "http://ecs.amazonaws.com/onca/xml"
   	    . "?Service=AWSECommerceService"
	   	. "&AssociateTag=" . AWS_US_ASSOCIATE_TAG
	   	. "&AWSAccessKeyId=" . AWS_ACCESS_KEY
   		. "&Operation=" . $Operation
   		. "&Version=" . $Version
   		. "&SearchIndex=VideoGames"
   		. "&BrowseNode=14219011"
   		. "&ResponseGroup=" . $ResponseGroup
   		. "&ItemPage=" . $page;

		$response = file_get_contents($request);
		$parsed_xml = simplexml_load_string($response);
		return $parsed_xml;
	}

	private function getPage($page, $locale){
		
		Validate::notNull($locale, "attempting to get page, when locale not defined");

		if(UK_LOCALE == $locale)
			return $this->AWSUKItemSearch($page);
		if(US_LOCALE == $locale)
			return $this->AWSUSItemSearch($page);
		
	}
	
	public function getItemPrice($itemId){
		
		$request = 
			"http://ecs.amazonaws.com/onca/xml?"
			. "Service=AWSECommerceService"
			. "&AWSAccessKeyId=" . AWS_ACCESS_KEY
			. "&Operation=ItemLookup"
			. "&ResponseGroup=ItemAttributes,Offers"
			. "&ItemId=" . $itemId;

		$response = file_get_contents($request);
		$parsed_xml = simplexml_load_string($response);
		
		return $parsed_xml->Items->Item->OfferSummary->LowestUsedPrice->FormattedPrice;			
	}
	
	public function getTotalResults($locale){
		
		Validate::notNull($locale, "attempting to get total results, when locale not defined");
				
		if (UK_LOCALE == $locale){
			return $this->AWSUKItemSearch("")->Items->TotalResults;
		}
		
		if (US_LOCALE == $locale){
			return $this->AWSUSItemSearch("")->Items->TotalResults;
		}
	}	
	
	public function getTotalPages($locale){
		
		Validate::notNull($locale, "attempting to get total pages, when locale not defined");
		
		if(UK_LOCALE == $locale)
			return $this->AWSUKItemSearch("")->Items->TotalPages;
		if(US_LOCALE == $locale)
			return $this->AWSUSItemSearch("")->Items->TotalPages;
	}
}
?>